#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

vector<string> to_tokens(const string& s) {
	string tok;
	vector<string> tokens;
	for(auto it=s.begin(); it != s.end(); ++it) {
		if(*it>='0' && *it<='9') {
			tok.push_back(*it);
		}
		else if(*it=='.') {
			tok.push_back(*it);
		}
		else if(*it==' ') {
			if(tok.length()>0) {
				tokens.push_back(tok);
				tok.clear();
			}
		}
		else {
			if(tok.length()>0) {
				tokens.push_back(tok);
				tok.clear();
			}
			tok.push_back(*it);
			tokens.push_back(tok);
			tok.clear();
		}
	}
	if(tok.length()>0) {
		tokens.push_back(tok);
		tok.clear();
	}
	return tokens;
}

vector<string> to_exp(const vector<string>& tokens) {
	stack<string> symbols;
	vector<string> exp;
	for(auto it=tokens.begin(); it!=tokens.end(); ++it) {
		if( *it=="(") {
			symbols.push(*it);
		}
		else if( *it == ")" ) {
			while(!symbols.empty()) {
				string c = symbols.top();
				symbols.pop();
				if(c=="(") {
					break;
				}
				exp.push_back(c);
			}
		}
		else if( *it == "+" || *it == "-" || *it=="/" || *it=="*" ) {
			while(!symbols.empty()) {
				string c = symbols.top();
				if( c == "(" ) {
					symbols.push(*it);
					break;
				}
				if( c == "-" || c == "+" ) {
					if( *it == "*" || *it == "/" ) {
						symbols.push(*it);
						break;
					}
				}
				symbols.pop();
				exp.push_back(c);
			}
			if(symbols.empty()) {
				symbols.push(*it);
			}
		}
		else {
			exp.push_back(*it);
		}
	}
	
	while(!symbols.empty()) {
		exp.push_back(symbols.top());
		symbols.pop();
	}
	return exp;
}

double calc(const vector<string>& exp) {
	stack<double> r;
	for(auto it=exp.begin(); it != exp.end(); ++it) {
		string t = *it;
		if( t[0] == '+' ||
			t[0] == '-' ||
			t[0] == '*' ||
			t[0] == '/' ) {
			double op2 = r.top();
			r.pop();
			double op1 = r.top();
			r.pop();
			switch(t[0]) {
			case '+':
				r.push(op1+op2);
				break;
			case '-':
				r.push(op1-op2);
				break;
			case '*':
				r.push(op1*op2);
				break;
			case '/':
				r.push(op1/op2);
				break;
			}
		}
		else {
			r.push( std::stod(t));
		}
	}
	return r.top();
}

string vec2str(const vector<string>& exp) {
	string r;
	for(auto it=exp.begin(); it!=exp.end();++it) {
		r.append(*it);
		r.push_back(' ');
	}
	return r;
}

int main()
{
	const string input = "13 + 4 * ( 2 + 1) + 5.0 - 26 / 7";
	//const string input = "1 + 4 * ( 2 + 1) + 5 - 26 / 7";
	vector<string> tokens = to_tokens(input);
	vector<string> exp = to_exp(tokens);
	cout << vec2str(exp) << "=" << calc(exp) << endl;
	return 0;
}
